import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  UseGuards,
  Req
} from '@nestjs/common'
import { OperationService } from './operation.service'
import { CreateOperationDto } from './dto/create-operation.dto'
import { UpdateOperationDto } from './dto/update-operation.dto'
import { ResponseMsg } from 'src/common/custom.decorator'
import { LoggerGuard } from 'src/logger/logger.guard'
import { EasyRequest } from 'types/global'

@Controller('auth/operation')
export class OperationController {
  constructor(private readonly operationService: OperationService) {}

  /**
   * 创建新操作
   * @param createOperationDto - 包含新操作信息的数据传输对象
   * @returns 创建的操作信息
   */
  @Post('create')
  @ResponseMsg('新增操作')
  @UseGuards(LoggerGuard)
  create(
    @Body() createOperationDto: CreateOperationDto,
    @Req() req: EasyRequest
  ) {
    return this.operationService.create(createOperationDto, req.user?.userId)
  }

  /**
   * 获取所有操作
   * @returns 操作列表
   */
  @Get('list')
  findAll(@Query() query: { pageNum: number; pageSize: number }) {
    return this.operationService.findAll(query)
  }

  /**
   * 根据ID查询单个操作
   * @param id - 操作ID
   * @returns 操作信息
   */
  @Get('detail')
  findOne(@Query('id') id: string) {
    return this.operationService.findOne(+id)
  }

  /**
   * 更新操作信息
   * @param id - 操作ID
   * @param updateOperationDto - 包含更新信息的数据传输对象
   * @returns 更新后的操作信息
   */
  @Patch('update')
  @ResponseMsg('更新操作')
  @UseGuards(LoggerGuard)
  update(
    @Body() updateOperationDto: UpdateOperationDto,
    @Req() req: EasyRequest
  ) {
    return this.operationService.update(updateOperationDto, req.user)
  }

  /**
   * 删除操作
   * @param id - 操作ID
   * @returns 删除结果信息
   */
  @Delete('delete')
  @ResponseMsg('删除操作')
  @UseGuards(LoggerGuard)
  remove(@Query('id') id: string, @Req() req: EasyRequest) {
    return this.operationService.remove(+id, req.user)
  }
}
